define(function(require){
	var $=require("jq.js");
	var cookie=require("pz-cookie.js");
	function ChargePay(chargePayBox){
		var $this$=this;
		//保存充值区域
		this.payArea=chargePayBox;
		//保存号码输入框
		this.telephoneNumber=$("#telephone-number-input",chargePayBox);
		//保存电话薄按钮
		this.phonebookBtn=$(".c-a",chargePayBox);
		//保存电话号码类型、地区提示区域
		this.areaOrtypeTips=$(".telephone-type",chargePayBox);
		//保存输入状态放大效果框
		this._inputStateZoomBox=$(".zoom-number-box",chargePayBox);
		//保存输入状态放大效果
		this._inputStateZoom=$(".zoom-number-box strong",chargePayBox);	
		//保存历史输入的列表框
		this.rechargeRecordBox=$(".recharge-record-box",chargePayBox);
		//保存历史记录列表
		this.rechargeRecordListBox=$(".recharge-record-list",chargePayBox);
		//保存话费面值输入框
		this.telephonePriceInput=$("#telephone-price",chargePayBox);
		//获取话费面额下拉框
		this.slideOptionsBox=$(".slide-down-con",chargePayBox);
		//获取销售价格
		this.minPrice=$(".mar",chargePayBox);
		//获取隐藏的优惠
		this.rechargeAmount=$("#recharge-amount",chargePayBox);
		//保存表单
		this.form=$("form",chargePayBox);
		//保存去重置按钮
		this.goPayBtn=$(".pay-btn",chargePayBox);
		/*****************添加事件*******************/
		//打开电话本 
		this.phonebookBtn.click(function(e){
			e.stopPropagation();
			$this$._rechargeRecordBoxShow();
		});
		//选择历史记录的号码
		this.rechargeRecordBox.mousedown(function(e){
			var targets = $(e.target);
			if(targets.attr("id") =="recharge-record-list"){
				return
			}else{
				if(e.target.tagName!="EM"){
					var num=Number($(e.target).attr("data-number"));
					if(num!=0){
						
						$this$.telephoneNumber.val(num);
					};
					$this$.rechargeRecordBox.slideUp("fast");
					$this$.telephoneNumber.keyup();	
					//更新面额可选
					$this$._updateIntoType();
				};
			}
		});
		//提交
		this.goPayBtn.click(function(){
			if($this$.telephoneNumber.attr("data-state")=="yes"){
				var isMobile = /^(?:13\d|15\d|18\d|17\d|14\d)-?\d{5}(\d{3}|\*{3})$/;
				if(!isMobile.test($this$.telephoneNumber.val())){
					$this$.areaOrtypeTips.html("<strong style='color:red;'>手机号码错误！</strong>");
				}else{
					$.post("/chargePay/accsegment.do",{mobilenum:$this$.telephoneNumber.val(),amount:parseInt($this$.telephonePriceInput.val())},function(ret){
							if($this$.telephoneNumber.attr("data-state")!="yes"){
								$this$.areaOrtypeTips.html("<strong style='color:red;'>该号段暂时无货！</strong>");
							};
							if(!ret.isOk){
								$this$.areaOrtypeTips.html("<strong style='color:red;'>该号段暂时无货！</strong>");
							};
							if(ret.success){
								//window.open("http://www.baidu") ;
								//$this$.form.submit();
								//$("#openWin").submit();
							};
					},"json");	
					$this$.form.submit();
				};
			}else{
				$this$.areaOrtypeTips.html("<strong style='color:red;'>输入手机号！</strong>");
			};
			
		});
		//手机号输入事件
		this.telephoneNumber.keyup(function(e){
			var value=this.value;
			$this$._inputTelephoneNum(value);
		}).focus(function(){
			if($(this).attr("data-state")!="yes"){
				var value=this.value;
				$this$._inputTelephoneNum(value);
			};
		}).blur(function(){
			//---------------------------------------------->
			//$this$.telephoneNumber.attr("data-state","yes");
			$this$._inputStateZoomBox.removeClass("zoom-number-box-show");
			$this$.rechargeRecordBox.slideUp("fast");
			//更新面额可选
			if($this$.telephoneNumber.attr("data-state")=="yes"){
				$this$._updateIntoType();
			};
		});
		//输入面额
		this.telephonePriceInput.click(function(e){
			e.stopPropagation();
			$(this).blur();
//			if($this$.telephoneNumber.attr("data-state")=="yes"){
//				$this$._showOptions(this);
//			}
//			else{
				//$this$.areaOrtypeTips.html("<strong style='color:red;'>手机号码输入错误</strong>");
				//$(this).blur();
			//};
			$this$._showOptions(this);
			$this$.rechargeRecordBox.slideUp("fast");
		});
		
		this.telephonePriceInput.next().click(function(e){
			e.stopPropagation();
			$(this).prev().click();
		});
		//点击选择面额
		this.slideOptionsBox.click(function(e){
			var thisObj = e.target;
			if($(thisObj).attr("static")=="0"){
				return;
			}
			else{
				//$this$.areaOrtypeTips.html("<strong>&nbsp;</strong>");
				//$this$.areaOrtypeTips.html("<strong style='color:red;'>"+ret.msg+"</strong>");
				$this$._hideOptions(e);
				//选择面额时候发送手机号和面额进行匹配
				$this$._sendPrice();
			}
		});
		//页面点击事件
		$(document).click(function(){
			$this$.slideOptionsBox.slideUp();
			$this$.rechargeRecordBox.slideUp("fast");
		});
		//初始化页面数据
		this._init();
	};
	ChargePay.prototype={
		//初始化
		_init:function(){
			this._getUserJiLu();
		},
		//替换_getCookieArr------------->
		_getUserJiLu:function(){
			var _this=this;
			//http://192.168.226.2:8087/chargePay/fillHistory.do
			$.get("/chargePay/fillHistory.do?timeStamp=" + new Date().getTime() +"",{},function(ret){
				var arrList=ret.length>4?ret.split("."):false;
				if(arrList){
					var newTelephone=arrList[0];
					_this.telephoneNumber.val(newTelephone);
					_this.telephoneNumber.keyup();
				}else{
					_this.rechargeRecordListBox.empty().append("<li data-number='0'>您还没有充值记录</li>");	
					//alert(typeof ret)	
				};
			});
		},
		//获取电话记录cookie
		_getCookieArr:function(){
			return cookie.get("PZ_USER_TELEPHONE")!=null?cookie.get("PZ_USER_TELEPHONE").split("."):false;
		},
		//设置电话记录到cookie里面
		_addCookie:function(){
			//PZ_USER_TELEPHONE
			var curTelephoneNum=this.telephoneNumber.val();
			var cookieVal=cookie.get("PZ_USER_TELEPHONE");
			//如果当前没有任何值
			if(cookieVal==null){
				cookie.set("PZ_USER_TELEPHONE",curTelephoneNum,365)
			}else{
				//如果当前号码不再cookie里面就添加
				//alert(cookieVal+":"+(curTelephoneNum.toString().indexOf(cookieVal)));
				if(cookieVal.toString().indexOf(curTelephoneNum)<0){
					cookie.set("PZ_USER_TELEPHONE",cookieVal+"."+curTelephoneNum,365);
				};
			};
		},
		//选择面额时候发送手机号和面额进行匹配
		_sendPrice:function(){
			var _this=this;
			//prodprice
			//->/chargePay/accsegment.do?mobilenum=13051089047&amount=100
			_this._setLoading(true);
			$.post("/chargePay/accsegment.do",{mobilenum:this.telephoneNumber.val(),amount:parseInt(this.telephonePriceInput.val())},function(ret){
				if(ret.success){
					_this.minPrice.text("￥"+ret.prodprice);
					_this.rechargeAmount.val(ret.prodprice);
					_this.areaOrtypeTips.html("<strong style='color:red;'>"+ret.isptype+"</strong>");
				}else{
					_this.areaOrtypeTips.html("<strong style='color:red;'>"+ret.msg+"</strong>");
					_this.telephoneNumber.attr("data-state","no");
				}
				_this._setLoading(false);
			},"json")	
		},
		
		
		//设置面额
		_setIntoVal:function(thisObj){
			this.telephonePriceInput.val($(thisObj).attr("data-value"));
		},
		//隐藏下俩列表
		_hideOptions:function(e){
			this._setIntoVal(e.target);
			this.slideOptionsBox.slideUp();
		},
		//显示下拉列表
		_showOptions:function(thisObj){
			this.slideOptionsBox.find("i").removeClass("selected");
			this.slideOptionsBox.find("[data-value="+$(thisObj).val()+"]").addClass("selected")
			this.slideOptionsBox.slideDown();
		},
		//输入号码时
		_inputTelephoneNum:function(value){
			//如果当前输入的不是数字
			if(this._isNum(value)){
				this._setToValue(value);
			};
			//如果当前不为空的，就启动放大细节
			if(this.telephoneNumber.val()!=""){
				this._setZoomVal(this.telephoneNumber.val());
				this.rechargeRecordBox.slideUp("fast");
			}else{
				//当为空时，标记
				this.telephoneNumber.attr("data-state","no");
				//设为默认值
				this.areaOrtypeTips.html("支持移动、联通、电信");
				//隐藏
				this._inputStateZoomBox.removeClass("zoom-number-box-show");
				//当值为空的时候调去cookie显示出来
				//this._rechargeRecordBoxShow();
			};
			if(value.length<11){
				//当为空时，标记
				this.telephoneNumber.attr("data-state","no");
			};
		},
		//显示历史记录
		_rechargeRecordBoxShow:function(){
			var _this=this;
			//http://192.168.226.2:8087/chargePay/fillHistory.do
			$.get("/chargePay/fillHistory.do?timeStamp=" + new Date().getTime() +"",{},function(ret){
				var arrList=ret.length>4?ret.split("."):false;
				if(arrList){
					_this.rechargeRecordListBox.empty();
					$(arrList).each(function(i,o){
						if(ret.length >= 5){
							_this.rechargeRecordListBox.addClass("long");
						}
						_this.rechargeRecordListBox.append("<li data-number='"+o+"'>"+o+"</li>");
					});
					
					//更新面额可选
					_this._updateIntoType();
					
				}else{	
					_this.rechargeRecordListBox.empty().append("<li data-number='0'>您还没有充值记录</li>");	
				};
				_this.rechargeRecordBox.slideDown("fast");
			});	
		},
		//放大细节
		_setZoomVal:function(value){
			//先显示放大框
			this._inputStateZoomBox.addClass("zoom-number-box-show");
			//用“-”分割
			this._splitValue(value);
		},
		_splitValue:function(value){
			var _this=this;
			var newVal="";
			for(var i=0,l=value.length;i<l;i++){
				if(i==3){
					newVal+="-";
				};
				if(i==7){
					newVal+="-";
				};
				newVal+=value.charAt(i);	
			};
			//设置分割后的值
			if(value.length<=11){
				this._inputStateZoom.text(newVal);
				if(value.length==11){
					this._inputStateZoomBox.removeClass("zoom-number-box-show");
					//当输入完成后发送手机号码  
					this._sendNum(value);
				};
			}else{
				this._inputStateZoomBox.removeClass("zoom-number-box-show");
				this.telephoneNumber.val(this._inputStateZoom.text().replace(/-/g,""));
			};
		},
		//发送号码获取参数
		_sendNum:function(value){
			var _this=this;
			//->  /chargePay/accsegment.do?mobilenum=13911457892
			$.post("/chargePay/accsegment.do",{mobilenum:value,amount:parseInt(this.telephonePriceInput.val())},function(ret){
				_this._success(ret)
			},"json");
		},
		//数据返回
		_success:function(ret){
			/*
			 * isptype
			 * success
			*/
			var _this=this;
			var slideOptionsBox = _this.slideOptionsBox;
			
			if (!ret.checkNum && typeof(ret.checkNum)!="undefined" && ret.checkNum!=0){
				var checkNum = ret.checkNum;
				for(var i = 0; i <checkNum.length; i ++){
					if(checkNum.charAt(i)=="1"){
						slideOptionsBox.find("i").eq(i).attr("static","1").removeClass("");
					}
					if(checkNum.charAt(i)=="0"){
						slideOptionsBox.find("i").eq(i).attr("static","0").addClass("dis").attr("title","暂时无货");;
					}
				}
			}
			
			if(ret.success){
				this.areaOrtypeTips.html("<strong style='color:red;'>"+ret.isptype+"</strong>")
				//返回成功后设置标记
				this.telephoneNumber.attr("data-state","yes");
				//设置优惠价格
				this.minPrice.text("￥"+ret.prodprice);
				this.rechargeAmount.val(ret.prodprice);
			}else{
				this.areaOrtypeTips.html("<strong style='color:red;'>"+ret.msg+"</strong>");
				this.telephoneNumber.attr("data-state","no");
			};
		},
		//更新面额是否有货置灰显示
		_updateIntoType:function(){
			var _this=this;
			_this._setLoading(true);
			$.post("/chargePay/accsegment.do",{mobilenum:_this.telephoneNumber.val(),amount:parseInt(_this.telephonePriceInput.val())},function(ret){
				if(ret.checkNum==undefined){
					return;
				}
				checkNum = ret.checkNum;
				for(var i = 0; i <checkNum.length; i ++){
					if(checkNum.charAt(i)=="1"){
						_this.slideOptionsBox.find("i").eq(i).attr("static","1").removeClass("dis");
					}
					if(checkNum.charAt(i)=="0"){
						_this.slideOptionsBox.find("i").eq(i).attr("static","0").addClass("dis").attr("title","暂时无货");;
					}
				}
				_this._setLoading(false);
			},"json")
		},
		//loading
		_setLoading:function(type){
			var _this=this;
			if(type){
				_this.payArea.append("<div class='pay-loading'></div><div class='pay-mask'></div>");
			}
			else{
				$(".pay-loading,.pay-mask").remove();
			}
		},
		//设置输入值到输入框
		_setToValue:function(value){
			this.telephoneNumber.val(value.slice(0,value.length-1));
		},
		//获取最后一次输入的字符是否是数字
		_isNum:function(str){
			var lastStr=str.charAt(str.length-1);
			return isNaN(Number(lastStr));
		}
	
	
	};
	var chargePay=new ChargePay($("#J_ChargePay"));
});